package com.glamour.hlcity.testng;



import com.glamour.hlcity.component.framework.registry.impl.ApplicationContextFactory;
import com.glamour.hlcity.testng.common.TestApplicationContextHolder;
import com.glamour.hlcity.testng.listener.AbstractInjectBeanTestListener;

import org.apache.log4j.LogManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * glamour测试实现Bean注入
 *
 * @author zhaoxing.liuzx
 */
public class HlcityInjectBeanTestListener extends AbstractInjectBeanTestListener {

    private static final String[] parentCofigLoacations = new String[]{
        "classpath*:META-INF/spring/dao.xml",
        "classpath*:META-INF/spring/hlcity-biz.xml",
        "classpath*:META-INF/spring/hlcity-component-task.xml",
        "classpath*:META-INF/spring/hlcity-component-framework.xml",
        "classpath*:META-INF/spring/hlcity-component-aop.xml",
        "classpath*:META-INF/data-source.xml"
       
    };
    
 // 加载log4j
	private void loadLog4j() {
		new DOMConfigurator().doConfigure(getLog4j(),
				LogManager.getLoggerRepository());
	}
	/**
	 * 该方法返回log4j的相对地址，子类通过继承该方法可以修改log4j资源文件
	 * 
	 * @return
	 */
	protected String getLog4j() {
		return "src/test/resources/META-INF/log4j.xml";
	}

    @Override
    public void afterApplicationContextLoad(ApplicationContext context) {
        super.afterApplicationContextLoad(context);
        loadLog4j();
        ApplicationContextFactory.putApplicationContext(TestApplicationContextHolder.instance().context());
    }

    @Override
    public ApplicationContext getParentApplicationConext() {
       // ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(parentCofigLoacations);
    	ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext(parentCofigLoacations);
        ctx.refresh();
        return ctx;
    }
}
